<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket Client Demo</title>
</head>
<body>

<button onclick="sendMsg()">发消息</button>
<button onclick="close()">关闭连接</button>

</body>

<script>
    //const socket = new WebSocket("wss://192.168.10.6:8081")
    const socket = new WebSocket("wss://localhost:8081")

    socket.onopen = function (event) {
        console.log("WebSocket链接已打开")
    }

    socket.onclose = function (event) {
        console.log("WebSocket链接已关闭")
    }

    socket.onmessage = function (event) {
        console.log("收到回复：" + event.data)
    }

    function sendMsg() {
        const message = {
            id: 1,
            title: 'Hello Server'
        };
        socket.send(JSON.stringify(message))
    }

    function close() {
        socket.close()
    }

</script>
</html>
